﻿using MVC_Project.BLL;
using Project.DAL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.BLL
{

    public class CartBll : BaseBll<milktea_cart>
    {
        public override void SetDal()
        {
            Dal = new CartDal();
        }

        public int AddCart(milktea_cart cart) {
            var one = Dal.Query().Where(c=> c.Dstatus == true).OrderByDescending(c => c.add_time).FirstOrDefault();

            bool panduan = true;

            if (one != null)
            {
                DateTime sj = (DateTime)one.add_time;

                DateTime today = DateTime.Today;
                DateTime tempToday = new DateTime(sj.Year, sj.Month, sj.Day);
                if (today == tempToday)
                    panduan = true;
                else
                    panduan = false;

                if (panduan)
                {
                    return one.cart_id;
                }
                else {
                    Dal.Add(cart);

                    Dal.SaveChanges();

                    var three = Dal.Query().Where(c => c.Dstatus == true).OrderByDescending(c => c.add_time).FirstOrDefault();

                    return three.cart_id;

                }

            }
            else { 
                Dal.Add(cart);

                Dal.SaveChanges();

                var two = Dal.Query().Where(c => c.Dstatus == true).OrderByDescending(c => c.add_time).FirstOrDefault();

                return two.cart_id;

            }

        }
    }
}
